#include"tcpsever.h"
using namespace std;

tcpsever::tcpsever(char *ip,unsigned short port,int pth_num)
{
	//构建服务器     bind    listen
	cout<<"tcpsever()"<<endl<<endl;
	mpth_num=pth_num;
   // mpth_num=3;
	int sockfd=socket(PF_INET,SOCK_STREAM,0);
	assert(sockfd!=-1);

	cout<<"you input:"<<ip<<" "<<port<<" "<<mpth_num<<endl<<endl;
	struct sockaddr_in ser;
	ser.sin_family=AF_INET;
	ser.sin_port=htons(port);
	ser.sin_addr.s_addr=inet_addr(ip);
   
	/*ser.sin_family=AF_INET;
	ser.sin_port=htons(6666);
    ser.sin_addr.s_addr=inet_addr("192.168.1.120");*/

	int res=bind(sockfd,(struct sockaddr*)&ser,sizeof(ser));
	assert(res!=-1);

	int res2=listen(sockfd,5);
	assert(res2!=-1);
	_listen_fd=sockfd;
	

	_base = event_base_new();


}

void tcpsever::showcli_num(void *arg)
{
	Ptcpsever mthis=(Ptcpsever)arg;
	map<int,int>::iterator it=mthis->_pth_event_map.begin();
	for(;it!=mthis->_pth_event_map.end();it++)
	 {  
		 cout<<"sockpair[0]:"<<it->first;
		 cout<<"    cli_num:"<<it->second <<endl;
	}
	cout<<endl;
}
void listen_cb(int fd,short event,void *arg)
{
	cout<<"listen_cb !"<<endl;
	Ptcpsever mthis=(Ptcpsever)arg;
	//accept    ->cli_fd       
	struct sockaddr_in cli;
	socklen_t len=sizeof(cli);
	int cli_fd=accept(fd,(struct sockaddr *)&cli,&len);
	cout<<"accept one cli suess!"<<endl;
	
	//在_pth_event_map   找到将听量最小的子线程
	mthis->showcli_num(mthis);

	map<int,int>::iterator it1=mthis->_pth_event_map.begin();
	map<int,int>::iterator it2=mthis->_pth_event_map.begin();
	while(it2!=mthis->_pth_event_map.end())
	{
		if((it2->second) <(it1->second))
			it1=it2;
	
	
		 it2++;
	}
	cout<<"find "<<it1->first<<"'s  is least!! "<<endl<<endl;
	send(it1->first,&cli_fd,4,0);
}

void sock_0_cb(int fd,short event,void *arg)
{
	cout<<"sock_0_cb"<<endl;
	Ptcpsever mthis=(Ptcpsever)arg;
	//recv  ->pth_num
	int  pth_num=-1;
	recv(fd,&pth_num,4,0);
	cout<<"get pth_num:"<<pth_num<<endl<<endl;
	mthis->_pth_event_map[fd] = pth_num;

	mthis->showcli_num(mthis);
}


void tcpsever::create_socketpair()
{
	cout<<"create_socketpair"<<endl;
	for(int i = 0; i<mpth_num; i++)
	{
		int arr[2];
		int res=socketpair(AF_LOCAL,SOCK_STREAM,0,arr);
		if(res<0)
		{
		cout<<"socketpair create error!"<<endl<<endl;
		exit(0);
		}

		//将socket0放入map表，对应线程监听量0
		_pth_event_map.insert(pair<int,int>(arr[0],0));
		//将sock_0加入到libevent
		struct event* sock_0_event = event_new(_base,arr[0],EV_READ|EV_PERSIST,sock_0_cb,this);
		event_add(sock_0_event,NULL);
		
		cout<<"sockpair[0]: "<<arr[0]<<"------  sockpair[1]:"<<arr[1]<<endl;
		//将socketpair保存起来
		_socketpair_base0.push_back(arr[0]);
		_socketpair_base1.push_back(arr[1]);
	
	}
	
}

void tcpsever::create_pth()
{
	cout<<"create_pth()"<<endl<<endl;
	for(int i = 0; i< mpth_num; i++)
	{
		sem_wait(&semcount);
		new Mpthread(_socketpair_base1[i]);
		sem_post(&semcount);
	}
}

void tcpsever::run()
{
	cout<<"tcpsever::run()"<<endl<<endl;
	//申请socketpair
	create_socketpair();

	//启动线程
	create_pth();

	//将listen_fd加入到 libevent

	struct event* listen_event = event_new(_base,_listen_fd,EV_READ|EV_PERSIST,listen_cb,this);


	event_add(listen_event,NULL);

	cout<<endl<<"tcpsever dispatch start!!!!"<<endl<<endl;
	event_base_dispatch(_base);//while(1){epoll_wait();}

	cout<<"tcpsever::run() over"<<endl<<endl;
}
